<!--
 * @name: 
 * @version: 1.1.1
 * @Author: 刘大可
 * @Date: 2022-03-30 12:08:30
 * @LastEditors: 刘大可
 * @LastEditTime: 2022-11-16 12:11:46
-->
<template>

    <view class="pages" :style="{'background-image':'url('+bg+')'}">
        <cu-custom bgColor="bgmain" :isBack="true">
            <view slot="backText"></view>
            <view slot="content">登录</view>
        </cu-custom>
        <!-- <view class="login"  @tap="bindgetuserinfo">登录</view> -->
        <view class="login"  @tap="showLogin">登录</view>
		<!-- isAuth -->
		<school-login :isAuth="isAuth"></school-login>
    </view>
</template>
<script>
var app = getApp();

export default {
    data() {
        return {
            bg:"",
			userInfo:{},
			isAuth:false
        }
    },
	onLoad(options) {
		// this.isAuth=true
		let  sys=uni.getStorageSync('sys')
		this.bg=sys.xcx_login_bg
		this.loginCheck()
		uni.$on('loginCell',  (data)=> {
			console.log(data);
			if (data.userInfo) {
				this.bindgetuserinfo(data)
			}
			this.isAuth=false
		})
	},
    methods: {
		getCode() {
			return new Promise((success, fail) => {
				uni.login({
					provider: "weixin",
					success, fail
				})
			})

		},
		async loginCheck() {
			let { code } = await this.getCode()
			console.log(code);
			app.globalData.util.request({
				url: 'login/loginCheck',
				data: {
					code: code || ""
				},
				success: (res) => {
					console.log("配置信息", res.data)
					if (res.data.status == 200) {
						if (res.data.token) {
							res.data.data = res.data.token
							// uni.setStorageSync('token', res.data.data.token)
							// this.islogin = true
							this.userInfo = res.data.data
						}
					}
					// console.log(res.data.data.sys.logo);


					// that.msg();
				}
			});
		},
		showLogin(){
			if (this.userInfo.u_id) {
				this.bindgetuserinfo({
					userInfo: {
						avatarUrl: this.userInfo.avatar,
						nickName: this.userInfo.nickname
					}
				})

			} else {
				this.isAuth = true
			}

		},
          //用户授权登录
        bindgetuserinfo(res={}) {
            console.log('打印', res);
            var that = this;
			uni.getUserProfile({
			    desc: '用于获取用户信息',
			    success(res) {
					console.log(res)
					
				
				}
			});
			
			uni.login({
				success(code) {
					// res.userInfo={}
					console.log(code)
					app.globalData.util.request({
						url: 'Login/miniLogin',
						data: {
							code: code.code,
							nickName: res.userInfo.nickName,
							avatar: res.userInfo.avatarUrl,
							spid: uni.getStorageSync('spid')
						},
						success(user) {
							uni.setStorageSync('userinfo', res.userInfo)
							that.loginuserinfo = res.userInfo
							that.setData({
								islogin: true
							})
							console.log(user.data.token)
							uni.setStorageSync('token', user.data.token)
							// that.getUser()
							var duration=1000
							uni.showToast({
								title:"登录成功", duration,
								mask: true,
								success: (res) => {
									setTimeout(() => {
										uni.navigateBack({ delta: 1 })
									}, duration);
								}
							});

						}
					})
				}
			})
        },
        
    },
}
</script>
<style lang="scss">
	page{
		// background-image: url('https://testa.fkynet.net/wximage/login_bg.jpg');
		
	}
.pages{
    display:flex;
    flex-direction: column;
    height: 100vh;
	background-size:auto 100% ;
	background-repeat: no-repeat;
	background-position: center;

}
    .login{
        width: 600rpx;
        height: 100rpx;
        background-color: $main_color;
        border-radius: 10rpx;
        margin-top: auto;
        margin-bottom: 150rpx;
        margin: auto auto 200rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        font-size: 40rpx;
       
    }
</style>